package cn.tedu.zx.album.adapter.controller;


import cn.tedu.zx.album.service.PictureService;
import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.common.restful.ResponseCode;
import cn.tedu.zx.pojo.court.dto.PictureAddNewDto;
import cn.tedu.zx.pojo.court.dto.PictureUpdateDto;
import cn.tedu.zx.pojo.court.vo.PictureDetailsVo;
import cn.tedu.zx.pojo.court.vo.PictureListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/picture/")
//通过knife4j配置IPI文档
@Api(tags = "04.照片模块")
@Validated/*未封装参数检查注解*/
public class PictureController {
    @Autowired
    private PictureService pictureService;

    @PostMapping("/addNew")
    @ApiOperation("添加照片")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/picture/insert')")
    /*@ApiIgnore注解忽略注解后的请求参数*/
    public JsonResult<Void> addNew(@Valid PictureAddNewDto pictureAddNewDto) {
        log.debug("开始执行{}", pictureAddNewDto);
        pictureService.addNew(pictureAddNewDto);
        log.debug("添加完成");
        return JsonResult.ok();//静态方法
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除照片")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/picture/delete')")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "照片id", required = true, dataType = "Long")}
    )
    public JsonResult<Void> delete(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        pictureService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改照片")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/picture/update')")
    public JsonResult<Void> update(@Valid PictureUpdateDto updateDto) {
        pictureService.updateById(updateDto);
        return JsonResult.ok();
    }

    @PostMapping("updateDescription")
    @ApiOperation("修改照片描述")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAnyAuthority('/picture/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "description", required = true, dataType = "String")
    })
    public JsonResult<Void> updateDescription(Long id, String description) {
        pictureService.updateDescription(id,description);
        return JsonResult.ok();
    }

    @PostMapping("/setCover")
    @ApiOperation("通过id将目标图片设置为封面")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('/picture/update')")
    @ApiImplicitParam(name = "id", value = "图片id", required = true, example = "8", dataType = "Long")
    public JsonResult<Void> setCover(Long id) {
        pictureService.setCover(id);
        return JsonResult.ok();
    }

    @GetMapping("/selectDetails")
    @ApiOperation("根据id查询照片详情")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "照片id", required = true, dataType = "Long")}
    )
    public JsonResult<PictureDetailsVo> selectByI(@Range(min = 1, message = "Id异常，请添加正确的id") @RequestParam Long id) {
        PictureDetailsVo pictureDetailsVo = pictureService.selectDetailsById(id);
        return JsonResult.ok(pictureDetailsVo);
    }


    @GetMapping("/list")
    @ApiOperation("根据相册id查照片列表")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "albumId", value = "相册id", required = true, dataType = "Long")})
    public JsonResult<JsonPage<PictureListVo>> list(@RequestParam Integer pageNum, Integer pageSize, Integer albumId) {

        if (pageNum == null) {
            pageNum = 1;
        }
        log.debug("开始处理请求{}", pageNum);
        JsonPage<PictureListVo> jsonPage = pictureService.list(pageNum, pageSize, albumId);
        log.debug("查询完成{}", jsonPage);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("/slideshow")
    @ApiOperation("在图片数据库寻找轮播图")
    @ApiOperationSupport(order = 700)
    public JsonResult<List<String>> slideshowList() {
        List<String> list = pictureService.slideshowList();
        return JsonResult.ok(list);
    }

    @PostMapping("/slideshowChange")
    @ApiOperation("设置轮播图值")
    @ApiOperationSupport(order = 800)
    @PreAuthorize("hasAnyAuthority('/picture/update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "slideshow", value = "轮播图值", required = true, dataType = "Integer"),
    })
    public JsonResult<Void> slideshowChange(@RequestParam Long id, @RequestParam Integer slideshow) {
        pictureService.slideshowChange(id, slideshow);
        return JsonResult.ok();
    }


}
